package com.xinyiweidai.xinyiweidai.util;


import android.annotation.SuppressLint;
import android.app.Application;
import android.content.SharedPreferences;

import com.xinyiweidai.xinyiweidai.Constants;
import com.xinyiweidai.xinyiweidai.MyApplication;
import com.xinyiweidai.xinyiweidai.bean.LoginBean;

/**
 * package：com.yijia.loveshop.com.lan.mylibrary.util
 * classname: SpUtil
 * create time: 2018/4/27 10:46
 * author: Lan
 * desc :  存储用户个人信息
 */

public class SpUtil {
    private static SpUtil spUtil;
    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;


    public static SpUtil getInstance() {
        if (spUtil == null) {
            spUtil = new SpUtil();
        }
        return spUtil;
    }


    @SuppressLint("CommitPrefEdits")
    private SpUtil() {
        sp = MyApplication.getInstance().getSharedPreferences(Constants.APP_NAME, Application.MODE_PRIVATE);
        editor = sp.edit();
    }


    public void clearUserInfo() {
        editor.clear().commit();
    }


    public void saveUserInfo(LoginBean.DataBean.UserinfoBean userBean) {

        //保存登录信息
        editor.putString("token",userBean.getToken()).commit();
        editor.putString("user_id",userBean.getUser_id()).commit();
        editor.putString("nickname",userBean.getNickname()).commit();
        editor.putString("mobile",userBean.getMobile()).commit();

        editor.putString("sfz",userBean.getSfz()).commit();//身份认证
        editor.putString("yhk",userBean.getYhk()).commit();//银行卡认证
        editor.putString("yys",userBean.getYys()).commit();//运营商认证
        editor.putString("txl",userBean.getTxl()).commit();//通讯录认证
        editor.putString("htyz",userBean.getHtyz()).commit();//活体认证
        editor.putString("allbind",userBean.getAllbind()).commit();//  所有已绑定1，没绑定完全0
        editor.putString("zhuangtai",userBean.getZhuangtai()).commit();//申请状态  0可借  1审核中  2待还款
    }


    public LoginBean.DataBean.UserinfoBean getUserInfo() {
       LoginBean.DataBean.UserinfoBean userBean = new LoginBean.DataBean.UserinfoBean();

       userBean.setToken(sp.getString("token","000000"));
       userBean.setUser_id(sp.getString("user_id",""));
       userBean.setNickname(sp.getString("nickname",""));
       userBean.setMobile(sp.getString("mobile",""));

       userBean.setSfz(sp.getString("sfz",""));
       userBean.setYhk(sp.getString("yhk",""));
       userBean.setYys(sp.getString("yys",""));
       userBean.setTxl(sp.getString("txl",""));
       userBean.setHtyz(sp.getString("htyz",""));
       userBean.setAllbind(sp.getString("allbind",""));
       userBean.setZhuangtai(sp.getString("zhuangtai",""));

       return userBean;
    }

    public void saveString(String key, String value) {
        editor.putString(key, value).commit();
    }

    public String getString(String key) {
       return sp.getString(key,"");
    }


    public void saveBoolean(String key, boolean value) {
        editor.putBoolean(key, value).commit();
    }

    public void saveInt(String key, int value) {
        editor.putInt(key, value).commit();
    }

    public boolean getLoginState() {
        return sp.getBoolean("isAutoLogin", false);
    }



}
